﻿using System;
using System.Globalization;
using System.IO;
using Duo1JHttpServer.Framework.Model;
using Duo1JHttpServer.Framework.Utils;

namespace Duo1JHttpServer.Framework.Server
{
    /// <summary>
    /// 静态处理器
    /// </summary>
    internal static class StaticHandler
    {
        /// <summary>
        /// 请求正常响应头
        /// </summary>
        public static void RequestSuccess(RequestModel requestModel)
        {
            if (requestModel == null)
            {
                throw new Exception("RequestModel is NULL!");
            }

            ConsoleUtils.WriteGreenLine($"200 OK: {requestModel.RequestUrl}!\n");

            string html = new HtmlFormatter().AddStrLine("HTTP/1.1 200 OK")
                .AddStrLine("Content-Type: text/html;charset=utf-8")
                .AddStrLine("Connection: close").GetHtmlResult();

            requestModel.OutputWriter.WriteLine(html);
        }

        /// <summary>
        /// 请求为空响应头
        /// </summary>
        public static void RequestNotFound(RequestModel requestModel)
        {
            if (requestModel == null)
            {
                throw new Exception("RequestModel is NULL!");
            }

            ConsoleUtils.WriteRedLine($"404 Not Found: {requestModel.RequestUrl}!\n");

            string html = new HtmlFormatter().AddStrLine("HTTP/1.1 404 File not found")
                .AddStrLine("Connection: close")
                .AddHtmlTagStart()
                .AddBodyTagStart()
                .AddLineHtml("h1", "404 Not Found")
                .AddLineHtml("p", $"Current Time: {DateTime.Now.ToString(CultureInfo.InvariantCulture)}")
                .AddLineHtml("p", $"Request Url: {requestModel.RequestUrl}")
                .AddBodyTagEnd()
                .AddHtmlTagEnd().GetHtmlResult();

            requestModel.OutputWriter.WriteLine(html);
        }

        /// <summary>
        /// 500错误返回
        /// </summary>
        public static void RequestServerError(RequestModel requestModel)
        {
            if (requestModel == null)
            {
                throw new Exception("RequestModel is NULL!");
            }

            ConsoleUtils.WriteRedLine($"500 Internal server error: {requestModel.RequestUrl}!\n");

            string html = new HtmlFormatter().AddStrLine("HTTP/1.1 500 Internal server error")
                .AddStrLine("Connection: close")
                .AddHtmlTagStart()
                .AddBodyTagStart()
                .AddLineHtml("h1", "500 Internal server error")
                .AddLineHtml("p", $"Current Time: {DateTime.Now.ToString(CultureInfo.InvariantCulture)}")
                .AddLineHtml("p", $"Request Url: {requestModel.RequestUrl}")
                .AddBodyTagEnd()
                .AddHtmlTagEnd().GetHtmlResult();

            requestModel.OutputWriter.WriteLine(html);
        }

        /// <summary>
        /// 500错误返回
        /// </summary>
        public static void RequestServerError(StreamWriter outputWriter, string errorStr = null)
        {
            if (outputWriter == null)
            {
                throw new Exception("StreamWriter is NULL!");
            }

            ConsoleUtils.WriteRedLine($"500 Internal server error!\n");

            HtmlFormatter htmlFormatter = new HtmlFormatter().AddStrLine("HTTP/1.1 500 Internal server error")
                .AddStrLine("Connection: close")
                .AddHtmlTagStart()
                .AddBodyTagStart()
                .AddLineHtml("h1", "500 Internal server error")
                .AddLineHtml("p", $"Current Time: {DateTime.Now.ToString(CultureInfo.InvariantCulture)}");
            if (errorStr != null && !"".Equals(errorStr))
            {
                htmlFormatter.AddStrLine(StringUtils.GetLinedHtml(errorStr));
            }

            htmlFormatter.AddBodyTagEnd()
                .AddHtmlTagEnd();

            outputWriter.WriteLine(htmlFormatter.GetHtmlResult());
        }
    }
}